﻿

(function() {
    
    AjaxControlFramework.Script.Utils.createNamespace('AjaxControlFramework.UpdateRegion');
    
    
    if (typeof window["__doPostBack"] === "function")
    {
        var standardPostBack = window["__doPostBack"];

        window["__doPostBack"] = function(eventTarget, eventArgument) 
        {        
            // Loop through UniqueID hierarchy of the eventTarget looking for a UniqueID that can be 
            // found in the registeredRegions collection. The first one found is that of the 
            // UpdateRegion that needs to have its HTML updated. If an UpdateRegion cannot be found, 
            // then a standard postback will be triggered.
            var updateRegion = null;
            
            var delimiterIndex = -1;
            var eventTargetCopy = eventTarget;
            
            while ((delimiterIndex = eventTargetCopy.lastIndexOf("$")) > -1)
            {
                updateRegion = AjaxControlFramework.UpdateRegion.registeredRegions[(eventTargetCopy = eventTargetCopy.substring(0, delimiterIndex))];
                
                if (updateRegion != null)
                {
                    break;
                }
            }
            
            
            if (updateRegion != null)
            {
                theForm.__EVENTTARGET.value = eventTarget;
                theForm.__EVENTARGUMENT.value = eventArgument;
                
                updateRegion.update(function() { 
                        theForm.__EVENTTARGET.value = "";
                        theForm.__EVENTARGUMENT.value = "";
                    }, function(exception) { 
                        theForm.__EVENTTARGET.value = "";
                        theForm.__EVENTARGUMENT.value = "";
                        AjaxControlFramework.Script.Utils.displayException(exception);
                    }, true
                );
            }
            else
            {
                standardPostBack(eventTarget, eventArgument);
            }
        };
    }
    else
    {
        var inputs = document.getElementsByTagName("input");
        
        for (var index = 0; index < inputs.length; index++)
        {
            if (inputs[index].onclick !== null)
            {
                inputs[index].onclick = function(e) {
                    this.onclick(e);
                    this.focus();
                };
            }
            else
            {
                inputs[index].onclick = function(e) {
                    this.focus();
                };
            }
        }
        
        
        document.forms[0].onsubmit = function(e) {
            var eventTarget = null;
            var updateRegion = null;
            var evt = window.event || e;
            
            if (document.activeElement)
            {
                eventTarget = document.activeElement.getAttribute("name");
            }
            else if (evt.explicitOriginalTarget)
            {
                eventTarget = window.selection.focusNode.name;
            }
            else 
            {
                return;
            }
                
            var delimiterIndex = -1;
            var eventTargetCopy = document.activeElement.name;
            
            while ((delimiterIndex = eventTargetCopy.lastIndexOf("$")) > -1)
            {
                updateRegion = AjaxControlFramework.UpdateRegion.registeredRegions[(eventTargetCopy = eventTargetCopy.substring(0, delimiterIndex))];
                
                if (updateRegion != null)
                {
                    break;
                }
            }
            
            
            if (updateRegion != null)
            {
                if (!document.forms[0].__EVENTTARGET)
                {
                    var eventTargetInput = document.createElement("input");
                    eventTargetInput.setAttribute("id", "__EVENTTARGET");
                    eventTargetInput.setAttribute("type", "hidden");
                    eventTargetInput.value = document.activeElement.name;
                    document.forms[0].appendChild(eventTargetInput);
                }
                else
                {
                    document.forms[0].__EVENTTARGET.value = document.activeElement.name;
                }
                
                
                if (!document.forms[0].__EVENTARGUMENT)
                {
                    var eventArgumentInput = document.createElement("input");
                    eventArgumentInput.setAttribute("id", "__EVENTARGUMENT");
                    eventArgumentInput.setAttribute("type", "hidden");
                    eventArgumentInput.value = document.activeElement.value;
                    document.forms[0].appendChild(eventArgumentInput);
                }
                else
                {
                    document.forms[0].__EVENTARGUMENT.value = document.activeElement.value;
                }
                
                updateRegion.update(function(result) { }, function(exception) { 
                    AjaxControlFramework.Script.Utils.displayException(exception);
                }, true);
                
                return false;
            }
        };
    }
    
    
    AjaxControlFramework.UpdateRegion = function(uniqueID) {
        AjaxControlFramework.AjaxControl.call(this, uniqueID);
    };
    
    AjaxControlFramework.UpdateRegion.registeredRegions = {};
    
    AjaxControlFramework.UpdateRegion.prototype = new AjaxControlFramework.AjaxControl(" ");
    AjaxControlFramework.UpdateRegion.prototype.constructor = AjaxControlFramework.UpdateRegion;    
    
    AjaxControlFramework.UpdateRegion.prototype.update = function(onSuccess, onFailure, autoUpdateHtml) {
        AjaxControlFramework.AjaxControl.prototype.update.apply(this, [{
            onSuccess: onSuccess,
            onFailure: onFailure,
            autoUpdateHtml: autoUpdateHtml
        }]);
    };
    
    delete AjaxControlFramework.UpdateRegion.prototype.globalUpdate;
    delete AjaxControlFramework.UpdateRegion.prototype.invoke;
    delete AjaxControlFramework.UpdateRegion.prototype.globalInvoke;
    
    AjaxControlFramework.UpdateRegion.prototype.register = function() {
        AjaxControlFramework.UpdateRegion.registeredRegions[this.getUniqueID()] = this;
        AjaxControlFramework.AjaxControl.prototype.register.apply(this);
    };
    
    AjaxControlFramework.UpdateRegion.prototype.unregister = function() {
        delete AjaxControlFramework.UpdateRegion.registeredRegions[this.getUniqueID()];
        AjaxControlFramework.AjaxControl.prototype.unregister.apply(this);
    };
    
})();